//
//  LaunchVC.m
//  UHealth
//
//  Created by gjtxz on 15/1/15.
//  Copyright (c) 2015年 gjtx. All rights reserved.
//

#import "LaunchVC.h"

#import "AppDelegate.h"

#import "FillInfoVC.h"


@interface LaunchVC ()<UIScrollViewDelegate>
{
    UIPageControl *_control;
    UIScrollView *zScrollView;

}


@end

@implementation LaunchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = kBlueColor;
//    
//    UIImage *image = [[UIImage imageNamed:@"ukang"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];//拉伸图片的时候让图片不变形
//    UIImageView *img = [[UIImageView alloc]initWithImage:image];
//    img.frame = CGRectMake(70, (self.view.frame.size.height - 65)/2, 180, 65);
//    [self.view addSubview:img];
//    
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//    btn.frame = CGRectMake(100, self.view.frame.size.height - 50-30, self.view.frame.size.width - 200, 30);
//    [btn.layer setMasksToBounds:YES];
//    [btn.layer setCornerRadius:5.0];//设置矩形是个圆角半径
//    [btn.layer setBorderWidth:1.0];//边框宽度
//    [btn.layer setBorderColor:[[UIColor whiteColor]CGColor]];
//    [btn setTitle:[NSString stringWithFormat:kLocal(@"launch"),0] forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
////    [self.view addSubview:btn];
//    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//        
//        if ([def boolForKey:kDidRegistered]) {
//            
//            UINavigationController *infoNav = [[UINavigationController alloc]initWithRootViewController:[[FillInfoVC alloc]init]];
//            infoNav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
//            infoNav.navigationBar.barTintColor = kBlueColor;
//            [self presentViewController:infoNav animated:YES completion:nil];
//            
//        }else{
//            AppDelegate *delegate = [UIApplication sharedApplication].delegate;
//            [delegate changRootVCToMain];
//        }
//    });
    
    
    UIScrollView *uiScrollview = [[UIScrollView alloc] init];
    uiScrollview.frame = self.view.bounds;
    
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    [self.view addSubview:uiScrollview];
    
    for (int i = 0; i < 3; i++) {
        NSString *imageName;
        if (VIEW_3_5_INCH) {//判断屏幕大小，选取合适的背景图
            imageName = [NSString  stringWithFormat:@"h960_%02d" ,i+1];
        }else if (VIEW_4_INCH){
            imageName = [NSString  stringWithFormat:@"w640_%02d" ,i+1];
        }else if (VIEW_4_7_INCH){
            imageName = [NSString  stringWithFormat:@"w750_%02d" ,i+1];
        }else if (VIEW_5_5_INCH){
            imageName = [NSString  stringWithFormat:@"w1242_%02d" ,i+1];
        }
        UIImage *image = [UIImage imageNamed: imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(i*width, 0, width, height);
        [uiScrollview addSubview:imageView];
    }
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(2*width+ 100, HEIGHT - 50-40, WIDTH - 200, 40);
    [btn.layer setMasksToBounds:YES];
    [btn.layer setCornerRadius:15.0];//设置矩形是个圆角半径
    [btn.layer setBorderWidth:1.0];//边框宽度
    [btn.layer setBorderColor:[kBlueColor CGColor]];
    [btn setTitle:[NSString stringWithFormat:kLocal(@"launch"),0] forState:UIControlStateNormal];
    [btn setTitleColor:kBlueColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [uiScrollview addSubview:btn];
    
    zScrollView = uiScrollview;
    uiScrollview.showsHorizontalScrollIndicator = NO;//是否显示拖动条
    uiScrollview.contentSize = CGSizeMake(3*width, height);
    uiScrollview.pagingEnabled = YES;
    uiScrollview.backgroundColor = [UIColor grayColor];
    uiScrollview.delegate = self;
    
    UIPageControl *control = [[UIPageControl alloc] init];
    control.numberOfPages = 3;
    control.bounds = CGRectMake(0, 0, 200, 50);
    control.center = CGPointMake(width*0.5, height-30);
    control.currentPage = 0;
    control.pageIndicatorTintColor = [UIColor grayColor];
    control.currentPageIndicatorTintColor = kBlueColor;
    _control = control;
    [_control addTarget:self action:@selector(onPointClick) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:control];

    
}

//记录是否第一次登录
-(void)BtnClick:(UIButton *)Btn
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setBool:YES forKey:@"AlreadyLaunch"];
    [def synchronize];
    [self presentViewController:[[FillInfoVC alloc]init] animated:YES completion:nil];
}

//拖动效果
- (void)onPointClick
{
    CGFloat offsetX = _control.currentPage * zScrollView.frame.size.width;
    [UIView animateWithDuration:0.3 animations:^{
        zScrollView.contentOffset = CGPointMake(offsetX, 0);
    }];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    int pageNum = scrollView.contentOffset.x / scrollView.frame.size.width;
    _control.currentPage = pageNum;
}


//-(void)BtnClick:(UIButton *)Btn
//{
//    
//    
//    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//    
//    if ([def boolForKey:kDidRegistered]) {
//        
//        UINavigationController *infoNav = [[UINavigationController alloc]initWithRootViewController:[[FillInfoVC alloc]init]];
//        infoNav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
//        infoNav.navigationBar.barTintColor = kBlueColor;
//        [self presentViewController:infoNav animated:YES completion:nil];
//        
//    }else{
//        AppDelegate *delegate = [UIApplication sharedApplication].delegate;
//        [delegate changRootVCToMain];
//    }
//    
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
